package uk.org.landeg.euler.problems;

import java.util.ArrayList;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.NumbersLib;

public class Problem145 extends AbstractProblem{
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem145());
		launcher.solve();		
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "How many reversible numbers are there below one-billion?";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 145;
	}

	@Override
	public Object solve() {
		notes.add("There are 120 reversible numbers below one thousand;");
		notes.add("This solution yields the correct answer, but needs serious optimization - takes over 1 min to run");
		notes.add("First optimization - if x is reversible, then rev(x) is also");
		int count = 0;
		long test = 0;
		ArrayList<Long> reversibleNumbers = new ArrayList<Long> ();
		boolean isReversible;
		for (long n = 1000 ; n < 5000 ; n++) {
			isReversible = true;
			if (n % 10 > 0) {
				test = n + NumbersLib.reverseDigits(n);
				do {
					if (test % 2 == 0) {
						isReversible = false;
						break;
					}
					test /= 10;
				} while (test > 0);
				if (isReversible) {
					reversibleNumbers.add(n);
				}
			}
			if (n > 0 && n % 100000000 == 0) {
				System.out.println(n);
			}
		}
		notes.add(reversibleNumbers.toString());
		return reversibleNumbers.size();
	}

}
